package controller;

import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.event.MouseMotionListener;
import java.util.List;

import gameobj.ElementType;
import gameobj.GameObject;
import gameobj.Player;
import manager.ElementManager;

public class GameListener implements MouseListener, MouseMotionListener{
	
	/**
	 * @author Passerby
	 * @说明
	 * 因为操作的对象只有玩家飞机，故将其提取成成员能减少获取的性能损耗。
	 * 一开始并不赋值，而是在后面的操作中将其寻找并赋值，且只需执行赋值一次
	 */
	private Player player = null;
	
	public void initPlayer() {
		if (player == null) {
			ElementManager em = ElementManager.get();
			List<GameObject> list = em.getList(ElementType.PLAYER);
			player = (Player) list.get(0);
		}
	}

	@Override
	public void mouseDragged(MouseEvent e) {
		initPlayer();
		player.setAttacking(true);
		player.setX(e.getX() - 30);
		player.setY(e.getY() - 30);
	}

	@Override
	public void mouseMoved(MouseEvent e) {
		initPlayer();
		player.setX(e.getX() - 30);
		player.setY(e.getY() - 30);
	}

	@Override
	public void mouseClicked(MouseEvent e) {

	}

	@Override
	public void mousePressed(MouseEvent e) {

	}

	@Override
	public void mouseReleased(MouseEvent e) {
		// 由于肯定是先有Press才有Release，故在此不需要initPlayer
		player.setAttacking(false);
		//player.getHurt();
	}

	@Override
	public void mouseEntered(MouseEvent e) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void mouseExited(MouseEvent e) {
		// TODO Auto-generated method stub
		
	}

}
